package dimension.starry.block.machine.simple.cupolaFurnace;

import dimension.starry.StarryDimension;
import dimension.starry.block.machine.simple.SimpleMachineRegister;
import dimension.starry.block.typeInterface.Machine;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class CupolaFurnaceFront extends Machine {

    private static final Text MACHINE_BROKEN = Text.translatable(
        StarryDimension.MOD_ID + ".gui.message.machine_not_complete");

    @Nullable
    @Override
    public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
        return new CupolaFurnaceTileEntity(pos, state);
    }

    @Override
    public ActionResult onUse(
        BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit
    ) {
        if (worldIn.isClient() || hand != Hand.MAIN_HAND) {
            return super.onUse(state, worldIn, pos, player, hand, hit);
        }

        boolean northHasBase = worldIn.getBlockState(pos.north())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_BASE.getBlock());
        boolean southHasBase = worldIn.getBlockState(pos.south())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_BASE.getBlock());
        boolean westHasBase = worldIn.getBlockState(pos.west())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_BASE.getBlock());
        boolean eastHasBase = worldIn.getBlockState(pos.east())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_BASE.getBlock());
        if (!northHasBase && !southHasBase && !westHasBase && !eastHasBase) {
            player.sendMessage(MACHINE_BROKEN, false);
            return ActionResult.FAIL;
        }

        boolean northHasHearth = worldIn.getBlockState(pos.north().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_HEARTH.getBlock());
        boolean southHasHearth = worldIn.getBlockState(pos.south().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_HEARTH.getBlock());
        boolean westHasHearth = worldIn.getBlockState(pos.west().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_HEARTH.getBlock());
        boolean eastHasHearth = worldIn.getBlockState(pos.east().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_HEARTH.getBlock());
        if (!northHasHearth && !southHasHearth && !westHasHearth && !eastHasHearth) {
            player.sendMessage(MACHINE_BROKEN, false);
            return ActionResult.FAIL;
        }

        boolean northHasMiddle = worldIn.getBlockState(pos.north().up().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_MIDDLE.getBlock());
        boolean southHasMiddle = worldIn.getBlockState(pos.south().up().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_MIDDLE.getBlock());
        boolean westHasMiddle = worldIn.getBlockState(pos.west().up().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_MIDDLE.getBlock());
        boolean eastHasMiddle = worldIn.getBlockState(pos.east().up().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_MIDDLE.getBlock());
        if (!northHasMiddle && !southHasMiddle && !westHasMiddle && !eastHasMiddle) {
            player.sendMessage(MACHINE_BROKEN, false);
            return ActionResult.FAIL;
        }

        boolean northHasTop = worldIn.getBlockState(pos.north().up().up().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_TOP.getBlock());
        boolean southHasTop = worldIn.getBlockState(pos.south().up().up().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_TOP.getBlock());
        boolean westHasTop = worldIn.getBlockState(pos.west().up().up().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_TOP.getBlock());
        boolean eastHasTop = worldIn.getBlockState(pos.east().up().up().up())
            .getBlock()
            .equals(SimpleMachineRegister.CUPOLA_FURNACE_TOP.getBlock());
        if (!northHasTop && !southHasTop && !westHasTop && !eastHasTop) {
            player.sendMessage(MACHINE_BROKEN, false);
            return ActionResult.FAIL;
        }

        return super.onUse(state, worldIn, pos, player, hand, hit);
    }
}
